<?php 
namespace Beerus\Modules\Designer\Controllers;

use App\Pockets\GridDataModel as pGridDataModel;
use App\Http\Controllers\Controller;
use Beerus\Modules\Designer\Actions\CancelToDecoCompany;
use Beerus\Modules\Designer\Actions\{AddRenewIntention, BindOperator, UnbindOperator, SetToDecoCompany};
use Beerus\Modules\DesignerList\Actions\ChangeMainArea;
use Doraemon\cache\Designer\DesignerViolation as cDesignerViolation;
use Doraemon\model\Designer as mDesigner;
use Doraemon\model\DesignerOperatorBind\DesignerOperatorBind;
use Doraemon\model\DesignerTeam;
use Doraemon\model\Operator;
use Doraemon\service\Designer\DesignerViolationRecord;
use Doraemon\service\Wechat\WechatBasic;
use Doraemon\tools\HhzEnv;
use Doraemon\model\Article;
use Doraemon\model\DesignerAuth;
use Doraemon\model\Im\RabbitMessage;
use Doraemon\model\Member;
use Doraemon\service\Designer\DesignerService;
use Doraemon\tools\Log;
use Illuminate\Http\Request;
use Encore\Admin\Facades\Admin;
use Encore\Admin\Grid;
use Encore\Admin\Layout\Content;
use Encore\Admin\Widgets\Tab;
use Illuminate\Support\MessageBag;
use Encore\Admin\Widgets\Form;
use Doraemon\service\User;
use Biz\pockets\excel\DownExcel;
use Encore\Admin\Grid\Displayers\DropdownActions;
use Encore\Admin\Widgets\Table;
use Doraemon\model\Designer\RenewIntentionLog as mDRILog;
use Doraemon\model\Designer\RenewIntention;
use Beerus\Modules\Designer\Actions\shortRecommendWord;

/**
 * 设计师列表
 *
 * @category   Module name
 * @package    PSR
 * @subpackage Documentation\API
 * @author     zhouyang  <zhouyang@haohaozhu.com>
 * @license    GPL http://www.haohaozhu.com
 * @link       http://www.haohaozhu.com
 */

class DesignerController extends Controller
{
    const PAGE_SIZE = 20;

    // 整屋案例、认证资料审核状态
    const AUDIT_STATUS = [
        0 => '待补充',
        1 => '审核中',
        2 => '未通过审核',
        3 => '通过审核'
    ];

    // 认证状态
    const AUTH_STATUS = [
        1 => '未认证',
//        2 => '待审核',
//        3 => '通过审核',
//        4 => '未通过审核',
        5 => '已认证',
        6 => '取消认证',
        7 => '认证到期',
    ];

    const PAY_STATUS = [
        0 => '未支付',
        1 => '已支付'
    ];

    // 认证类型
    const PLAN_TYPE = [
        1 => '付费认证',
        2 => '青年计划',
        3 => '白名单',
        7 => '免费机构所属',
    ];

    // 案例、资料审核状态
    const STATUS_AUDIT_SUPPLEMENT = 0;
    const STATUS_AUDIT_WAIT = 1;
    const STATUS_AUDIT_FAIL = 2;
    const STATUS_AUDIT_PASS = 3;

    const STATUS_AUTH = [
        '' => '全部',
        1 => '未认证',
        5 => '已认证',
        6 => '取消认证',
        7 => '认证过期'
    ];

    const STATUS_AUDIT = [
        '' => '全部',
        0 => '待补充',
        1 => '审核中',
        2 => '未通过审核',
        3 => '通过审核'
    ];

    const IS_TEST = [
        0 => '非测试号',
        1 => '测试号'
    ];

    const IS_SUBSCRIBE_OFFICIAL_ACCOUNT = [ // 是否关注微信服务号
        0 => '未关注',
        1 => '已关注'
    ];

    const CASE_REASONS = [
        '',
        '整屋案例不符合规范',
        '效果图',
        '非家装（样板间、民宿、工装、自宅等）',
        '疑似盗图',
        '作品不足（至少需上传至少2件作品）'
    ];

    const INFO_REASONS = [
        '',
        '手持身份证照片不符合规范',
        '身份证正面照片不符合规范',
        '身份证反面照片不符合规范',
        '申请公函与申请身份不对应',
        '申请公函没有填写时间',
        '营业执照照片不符合规范（须是原件）',
        '符合免费「青年计划」政策'
    ];

    public function __construct()
    {
        parent::__construct();
        Admin::script('$(".pjax-container").attr("id","pjax-container");');
    }

    // 设计师列表
    public function index(Request $request, Content $content)
    {

        $request_data = $request->all();

        $page_size = $request->input("per_page", self::PAGE_SIZE);
        $page = $request->input("page", 1);

	    $condition = [
		    'auth_status' => DesignerAuth::STATUS_AUTH_COMPLETE
	    ];
//        if (!empty($request_data['auth_status'])) {
//            $condition['auth_status'] = $request_data['auth_status'];
//        }

        if ($request_data['is_test'] != '') {
            $condition['is_test'] = $request_data['is_test'];
        }

        if (!empty($request_data['uid'])) {
            $condition['uid'] = $request_data['uid'];
        }

        if (!empty($request_data['have_recommend_text']) && count($request_data['have_recommend_text']) < 2) {
            $condition['have_short_recommend'] = $request_data['have_recommend_text'][0];
        }

        if (!empty($request_data['nick'])) {
//            $uid = User::getUidByNick($request_data['nick']);
//            if ($uid) {
//                $condition['uid'] = $uid;
//            }
	        $condition['nick[~]'] = $request_data['nick'];
        }

        if (!empty($request_data['designer_type'])) {
            $condition['designer_type'] = $request_data['designer_type'];
        }

        if (!empty($request_data['plan_type'])) {
            $condition['plan_type'] = $request_data['plan_type'];
        }

        if ($request_data['house_article_audit_status'] != '') {
            $condition['house_article_audit_status'] = $request_data['house_article_audit_status'];
        }

        if ($request_data['identity_info_audit_status'] != '') {
            $condition['identity_info_audit_status'] = $request_data['identity_info_audit_status'];
        }

        if (!empty($request_data['ability_tag'])) {
            $ability_tag = explode(' ', $request_data['ability_tag']);
            if ($ability_tag) {
                $condition["OR"] = [
                    "ability_tag_al[~]" => $ability_tag,
                    "ability_tag_op[~]" => $ability_tag
                ];
            }
        }

        if (!empty($request_data['operator_id'])) {
            $condition['operator_id'] = $request_data['operator_id'];
        }
        $total = 0;
        if (!empty($request_data['operator_id'])) {
            $isBind = in_array(0, $request_data['operator_id']) ? 0 : 1;
            $lists = DesignerAuth::getListWithOperatorId($condition, $page, $page_size, $isBind);
            $total = DesignerAuth::countWithOperatorId($condition, $isBind);
        } else {
            $condition['ORDER'] = ['id' => 'DESC'];
            $condition['LIMIT'] = [
                ($page - 1) * $page_size,
                $page_size
            ];

            $lists = DesignerAuth::select('*', $condition);
            if (!empty($lists)) {
                unset($condition['ORDER']);
                unset($condition['LIMIT']);
                $total = DesignerAuth::count($condition);
            }
        }
        if (!empty($lists)) {
            $this->formatDesignerList($lists);
        }

        $operatorList = Operator::getOperatorWithShowNameList();
        $operators = array_combine(array_column($operatorList, 'id'), array_column($operatorList, 'operator_show_name'));
        $operators[0] = '未绑定运营';
        $gridModel = new pGridDataModel($lists, $total, [
            'perPage' => $page_size
        ]);

        $search_params = http_build_query($condition);
        $gridList = Admin::grid(
            $gridModel,
            function (Grid $grid) use ($gridModel, $search_params, $operators) {
                $grid->setActionClass(DropdownActions::class);
                $grid->paginate($gridModel->getPerPage());
                $grid->disableExport();
                $grid->disableRowSelector();
                $grid->disableCreateButton();
                // 添加导出按钮
                $grid->tools(function (Grid\Tools $tool) use ($grid, $search_params) {
                    $url = url('/admin/Designer/export_phone?'. $search_params);
                    $button = '<div class="btn-group download" style="margin-right: 10px">
                        <a class="btn btn-sm btn-primary" target="_blank" href="'.$url.'">
                        <i class="fa fa-download"></i>
                        <span class="hidden-xs">导出手机号</span></a>
                    </div>';
                    $tool->append($button);

                    $metabase_button = '<div class="btn-group download" style="margin-right: 10px"> <a class="btn btn-sm btn-twitter" title="高级筛选" target="_blank" href="https://metabase.haohaozhu.me/question/3135"><i class="fa fa-filter"></i><span class="hidden-xs"> 高级筛选</span></a> </div>';
                    $tool->append($metabase_button);
                });

                $grid->filter(function (Grid\Filter $filter) use ($operators) {
                    $filter->disableIdFilter();
                    $filter->column(1 / 2, function ($filter) {
                        $filter->equal('uid', '用户UID')->placeholder('请输入uid');
                    });
                    $filter->column(1 / 2, function ($filter) {
                        $filter->equal('nick', '用户昵称')->placeholder('请输入昵称');
                    });
                    $filter->column(1 / 2, function ($filter) {
                        $filter->equal('designer_type', '设计师类别')->radio([
                            '' => '全部',
                            1 => '个人',
                            2 => '机构',
                            3 => '在职个人',
                            4 => '机构子账号'
                        ]);
                    });
                    $filter->column(1 / 2, function ($filter) {
                        $filter->equal('plan_type', '认证类型')->radio([
                            '' => '全部',
                            1 => '付费认证',
                            2 => '青年计划',
                            3 => '大V白名单'
                        ]);
                    });
                    $filter->column(1 / 2, function ($filter) {
                        $filter->equal('house_article_audit_status', '案例审核状态')->select(self::STATUS_AUDIT);
                    });
                    $filter->column(1 / 2, function ($filter) {
                        $filter->equal('identity_info_audit_status', '资料审核状态')->select(self::STATUS_AUDIT);
                    });
//                    $filter->column(1 / 2, function ($filter) {
//                        $filter->equal('auth_status', '认证状态')->select(self::STATUS_AUTH);
//                    });
                    $filter->column(1 / 2, function ($filter) {
                        $filter->equal('is_test', '是否测试号')->select(self::IS_TEST);
                    });
                    $filter->column(1 / 2, function ($filter) use ($operators) {
                        $filter->equal('operator_id', '对接运营')->checkbox($operators);
                    });
                    $filter->column(1 / 2, function ($filter){
                        $filter->equal('have_recommend_text', '是否设置了一句话推荐')->checkbox([
                            1 => '有',
                            9 => '没有',
                        ]);
                    });
                    $filter->column(1 / 2, function ($filter) {
                        $filter->equal('ability_tag', '能力标签')->placeholder('多个标签以空格分隔');
                    });
                    $filter->expand();
                });

                $grid->column("uid", '用户UID');
                $grid->column('nick', '用户昵称')->expand(function ($model) {
                    return new Table(['分层', '等级', '套餐', '推荐指数', '7天内咨询量', '区间','一句话推荐文案'], [$this->flow_pool_info]);
                }, true);
                $grid->column('operator_name', '对接运营');
                $grid->column("designer_type", '申请身份')->display(function () {
                    return DesignerAuth::DESIGNER_TYPE[$this->designer_type];
                });
	            $grid->column('belong_to', '所属机构')->display(function () {
					return $this->belong_to;
	            });
                $grid->column("create_time", '提交申请时间')->display(function () {
                    return $this->create_time;
                });

                $grid->column("case_num", '整屋案例数');
                $grid->column("im", '咨询数')->width(140)->display(function () {
                    $link = "https://frieza.haohaozhu.me/admin/im/msg_list?&from_user_group=0&from_user=&to_user_group=0&to_user={$this->uid}";
                    return '<a target="_blank" href="'.$link.'"> ' . $this->im . '</a>';
                });
                $grid->column("intention_level", '续费意愿');
                Admin::style('.column-ability_tag .label-default {display: inline-block;}');
                $grid->column("ability_tag", '能力标签')->display(function () {
                    return array_filter(array_merge(explode(',', $this->ability_tag_al), explode(',', $this->ability_tag_op)));
                })->label('default')->style("width:50px;");
                $grid->column("house_article_audit_status", '案例审核状态')->display(function () {
                    return self::AUDIT_STATUS[$this->house_article_audit_status];
                });

                $grid->column("update_time", '最近一次修改时间')->display(function () {
                    if (empty($this->update_time)) {
                        return '-';
                    }
                    return $this->update_time;
                });

                $grid->column("identity_info_audit_status	", '资料审核状态')->display(function () {
                    return self::AUDIT_STATUS[$this->identity_info_audit_status ];
                });

                $grid->column("plan_type", '认证类型')->display(function () {
                    return self::PLAN_TYPE[$this->plan_type];
                });

                $grid->column("auth_time", '认证时间')->display(function () {
                    if (empty($this->auth_time)) {
                        return '-';
                    }
                    return date('Y-m-d H:i:s', $this->auth_time);
                });

                $grid->column("expire_time", '到期时间')->display(function () {
                    $expireTimeTxt = !empty($this->expire_time) ? $this->expire_time : '-';
                    if (cDesignerViolation::isInTagPool(DesignerViolationRecord::getViolationCacheTag(DesignerViolationRecord::PUNISH_TYPE_NOT_ALLOW_RENEW), $this->uid)) {
                        $expireTimeTxt .= "<br><span style='color:red'>(".DesignerViolationRecord::PUNISH_TYPE_TXT_LIST[DesignerViolationRecord::PUNISH_TYPE_NOT_ALLOW_RENEW].")</span>";
                    }
                    return $expireTimeTxt;
                });

                $grid->column("auth_status", '认证状态')->display(function () {
                    if (empty($this->auth_status)) {
                        return '-';
                    }
                    return self::AUTH_STATUS[$this->auth_status];
                });

                $grid->column('is_pay', '支付状态')->display(function () {
                    return self::PAY_STATUS[$this->is_pay];
                });

                $grid->column('is_test', '是否测试号')->display(function () {
                    return self::IS_TEST[$this->is_test];
                });

                $grid->column('remark', '备注');

                $grid->actions(function (Grid\Displayers\Actions $actions) {
                    $actions->disableDelete();
	                $list = [
		                'zhouyang',
	                ];
	                if(!in_array(Admin::user()->username, $list)) {
		                $actions->disableEdit();
	                }
                    $actions->disableView();
                    // 如果当前登录用户是该设计师的运营则显示取消认领，
                    // 否则，该设计师没有其他运营认领的则显示认领
                    if ($actions->row['operator_name'] == Admin::user()->username) {
                        $actions->add(new UnbindOperator);
                    } elseif ($actions->row['operator_name'] === '') {
                        $actions->add(new BindOperator);
                    }
                    if (in_array(Admin::user()->username,['zhangzhijie','hujingdong','zhiyu'])){
                        $actions->add(new ChangeMainArea);
                    }
                    //一句话推荐逻辑
                    $actions->add(new shortRecommendWord);
                    $designer_info = mDesigner::getInfo($actions->row['uid']);
                    if (!empty($designer_info)) {
                        if ($designer_info['to_deco_company'] == mDesigner::TO_DECO_COMPANY_YES) {
                            $actions->add(new CancelToDecoCompany());
                        } elseif ($designer_info['to_deco_company'] == mDesigner::TO_DECO_COMPANY_NO) {
                            $actions->add(new SetToDecoCompany());
                        }
                    }
                    $actions->add(new AddRenewIntention());
                });
            }
        );

        $tab = new Tab();
        $tab->addLink('设计师列表', '/admin/Designer/list', true);
        $tab->addLink('续费意愿记录', '/admin/Designer/intentionLog');
        return $content
            ->header('设计师列表')
            ->description(' ')
            ->breadcrumb(['text' => '设计师列表'])
            ->body($tab)
            ->row($gridList);
    }

    public function formatDesignerList(&$designerList) {
        $user_ids = array_column($designerList, 'uid');
	    $aDesignerTeam = DesignerTeam::select(['org_uid', 'member_uid'], [
		    'member_uid' => $user_ids,
		    'status' => DesignerTeam::STATUS_NORMAL
	    ]);
	    $aMapMemberOrg = $aOrgUids = [];
	    foreach ($aDesignerTeam as $item) {
			$aMapMemberOrg[$item['member_uid']] = $item['org_uid'];
			$aOrgUids[] = $item['org_uid'];
	    }
	    $user_ids = array_merge($user_ids, $aOrgUids);
        $member_info = Member::getUsers($user_ids);

        // 续费意愿
        $aRenewIntentionList = RenewIntention::getListByUids($user_ids);
        $aIntentionAssocUidList = array_column($aRenewIntentionList, 'intention_level', 'uid');

        $im = RabbitMessage::listMessageCountByGroup($user_ids);
        $map_im = [];
        foreach ($im as $item) {
            $map_im[$item['to_uid']] = (int)$item['message_count'];
        }
        $user_ids = implode(',', $user_ids);
        $status = implode(',', [Article::STATUS_NORMAL, Article::STATUS_PRIVATE, Article::STATUS_WAIT_CHECK]);
        $sql = "select uid, COUNT(1) as case_num from hhz_article where uid IN (". $user_ids .") AND status IN (". $status .") group by uid";
        $article_info = Article::query($sql)->fetchAll();
        $map_uid_case = [];
        if (!empty($article_info)) {
            foreach ($article_info as $item) {
                $map_uid_case[$item['uid']] = (int)$item['case_num'];
            }
        }
        foreach ($designerList as &$item) {
            $item['nick'] = empty($member_info[$item['uid']]['nick']) ? '' : $member_info[$item['uid']]['nick'];
            $item['case_num'] = empty($map_uid_case[$item['uid']]) ? 0 : $map_uid_case[$item['uid']];
            $item['im'] = empty($map_im[$item['uid']]) ? 0 : $map_im[$item['uid']];
            $operator_info = DesignerOperatorBind::get(
                ['admin_name'],
                [
                    'uid' => $item['uid'],
                    'data_status' => DesignerOperatorBind::STATUS_NORMAL
                ]);
            $item['operator_name'] = !empty($operator_info) ? $operator_info['admin_name'] : '';
            $item['flow_pool_info'] = DesignerService::getFlowPoolInfo($item['uid']);
            $item['expire_time'] = !empty($item['expire_time']) ? date('Y-m-d H:i:s', $item['expire_time'])  : '';
            $belong_to = '';
            if ((DesignerAuth::DESIGNER_TYPE_COMPANY_BYPASS === (int)$item['designer_type']) && !empty($aMapMemberOrg[$item['uid']])) {
	            $org_uid = $aMapMemberOrg[$item['uid']];
	            $org_nick = empty($member_info[$org_uid]['nick']) ? '' : $member_info[$org_uid]['nick'];
	            $belong_to = "<b>uid：</b><span style='color: chocolate'>$org_uid</span><br/><b>nick：</b><span style='color: chocolate'>$org_nick</span>";
            }
            $item['belong_to'] = $belong_to;
            $item['intention_level'] = mDRILog::INTENTION_LEVEL_LIST[$aIntentionAssocUidList[$item['uid']]];
        }
        unset($item);
    }

    /**
     * 新增、修改
     * @param Request $request
     * @param Content $content
     * @param int $id
     * @return Content
     */
    public function edit(Request $request, Content $content, $id = 0)
    {
        $request = $request->all();

        if ($id == 0) {
            $is_create = true;
            $header = '新增';
        } else {
            $is_create = false;
            $header = '修改';
        }

        $tab = new Tab();
        $tab->add('设计师信息', self::editAction($request, $id, $is_create));
        return $content->header($header)
            ->breadcrumb([
                'text' => '设计师列表',
                'url' => route('designer.list')
            ], ['text' => $header])->row($tab);
    }

    public function editAction($request, $id = 0, $isCreate = false)
    {
        if ($isCreate) {
            $data = [];
            $form = new Form($data);
            $form->text('uid', '用户UID')->setWidth(4, 2)->required('不能为空');
        } else {
            $data = DesignerAuth::get('*', ['id' => $id]);
            $data['auth_time'] = empty($data['auth_time']) ? '' : date('Y-m-d H:i:s');
            $data['expire_time'] = empty($data['expire_time']) ? '' : date('Y-m-d H:i:s');
            $form = new Form($data);
            $form->hidden('id');
            $form->text('uid', '用户UID')->setWidth(4, 2)->readOnly();
        }

        $form->text('company_name', '所属公司名称')->setWidth(4, 2);
        $form->text('enterprise_name', '企业名称')->setWidth(4, 2);
        $form->select('designer_type', '设计师类别')->setWidth(4, 2)->options(DesignerAuth::DESIGNER_TYPE);
        $form->select('house_article_audit_status', '案例审核状态')->setWidth(4, 2)->options(self::AUDIT_STATUS);
        $form->select('reason2', '案例未通过的理由')->setWidth(4, 2)->options(self::CASE_REASONS);
        $form->select('identity_info_audit_status', '资料审核状态')->setWidth(4, 2)->options(self::AUDIT_STATUS);
        $form->select('reason', '资料未通过的理由')->setWidth(4, 2)->options(self::INFO_REASONS);
        $form->select('plan_type', '认证类型')->setWidth(4, 2)->options(self::PLAN_TYPE);
        $form->datetime('auth_time', '认证时间')->setWidth(4, 2);
        $form->datetime('expire_time', '到期时间')->setWidth(4, 2);
        $form->select('auth_status', '认证状态')->setWidth(4, 2)->options(self::AUTH_STATUS);
        $form->select('is_pay', '支付状态')->setWidth(4, 2)->options(self::PAY_STATUS);
        $form->select('is_test', '是否测试号')->setWidth(4, 2)->options(self::IS_TEST);
        $form->textarea('remark', '补充说明')->setWidth(4, 2)->options(self::IS_TEST);

        $form->action('/admin/Designer/list/save');
        return $form->render();
    }

    public function editSubmit(Request $request)
    {
        $request = $request->all();

        $data = [
            'uid' => $request['uid'],
            'company_name' => $request['company_name'],
            'enterprise_name' => $request['enterprise_name'],
            'designer_type' => $request['designer_type'],
            'house_article_audit_status' => $request['house_article_audit_status'],
            'reason2' => $request['reason2'],
            'identity_info_audit_status' => $request['identity_info_audit_status'],
            'reason' => $request['reason'],
            'plan_type' => $request['plan_type'],
            'auth_time' => strtotime($request['auth_time']),
            'expire_time' => strtotime($request['expire_time']),
            'auth_status' => $request['auth_status'],
            'is_pay' => $request['is_pay'],
            'is_test' => $request['is_test'],
            'remark' => $request['remark'],
            'admin_id' => Admin::user()->id
        ];

        if (isset($request['id'])) {
            $data['update_time'] = date('Y-m-d H:i:s');
            $id = $request['id'];

            $rs = DesignerAuth::update($data, [
                'id' => $id
            ]);
        } else {
            $same = DesignerAuth::getByUId($request['uid']);

            if (!empty($same)) {
                $error = new MessageBag([
                    'title' => '该用户已存在'
                ]);

                return back()->with(compact('error'));
            }

            $data['crete_time'] = date('Y-m-d H:i:s');

            $rs = DesignerAuth::create($data);
        }

        if ($rs) {
            if (isset($request['id'])) {
                $title = '修改成功';
            } else {
                $title = '添加成功';
            }

            $success = new MessageBag([
                'title' => $title
            ]);

            return redirect('/admin/Designer/list')->with(compact('success'));
        } else {
            if (isset($request['id'])) {
                $title = '修改失败';
            } else {
                $title = '添加失败';
            }

            $error = new MessageBag([
                'title' => $title
            ]);

            return back()->with(compact('error'));
        }
    }

    public function exportPhone(Request $request)
    {
        $list = [
            'yangjing',
            'renyuanxia',
        ];
        if(HhzEnv::isProduction() && !in_array(Admin::user()->username, $list)) {
            $title = '您没有权限操作';
            $error = new MessageBag([
                'title' => $title
            ]);

            return back()->with(compact('error'));

        }
        $request_data = $request->all();
        $lists = DesignerAuth::select('*', $request_data);
        $lists = is_array($lists) ? $lists : [];

        $data = [];
        if (!empty($lists)) {
            $user_ids = array_column($lists, 'uid');
            $member_info = Member::getUsers($user_ids);

            foreach ($lists as $item) {
                $data[] = [
                    'uid' => $item['uid'],
                    'nick' => empty($member_info[$item['uid']]['nick']) ? '' : $member_info[$item['uid']]['nick'],
                    'phone' => $item['phone'],
                ];
            }
        }

        // 设置列宽
        $header = [
            '用户UID', '用户昵称', '手机号',
        ];
        $letter = range('A', 'C');
        $excel_obj = new DownExcel();
        $excel_obj->setFileName('设计师手机号 - ' . date('YmdHis'));
        $excel_obj->setSheetTitle('设计师手机号');

        // 设置列宽
        $widths = [
            'A' => 12,
            'B' => 22,
            'C' => 18,
        ];

        $excel_obj->setWidth($widths);
        $excel_obj->setDefaultStyleAlignment();

        foreach ($header as $key => $value) {
            // 设置字体
            $excel_obj->setFrontName($letter[$key]);
            // 设置加粗
            $excel_obj->setFrontBold("$letter[$key]1");
            // 设置大小
            $excel_obj->setFrontSize($letter[$key]);
        }

        $excel_obj->create($data, $header);
    }
}
